package com.deyuanyun.pic.ctrl.controller.dto.validata;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;

import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.web.RequestResponseContext;
import com.deyuanyun.pic.common.validator.vinterface.ValidateInterface;
import com.deyuanyun.pic.ctrl.controller.dto.RepairEQPTRecordExcelVO;
import com.deyuanyun.pic.ctrl.controller.dto.RepairEQPTSuggestExcelVO;
import com.deyuanyun.pic.ctrl.enumeration.EqptTypeEnum;
import com.deyuanyun.pic.ctrl.service.EQPTBaseService;
import com.deyuanyun.pic.settings.controller.pipe.vo.PipeLocationDTO;
import com.deyuanyun.pic.settings.domain.util.DomainFieldReflectUtil;
import com.deyuanyun.pic.settings.service.pipe.PipeLocationService;

/**
 * 验证设备自定义编号
 * 
 * @author ChenKui
 * @date 2016-02-22
 */
public class EQPTRequestValidata implements ValidateInterface {

	@Override
	public <T, E,C> boolean apply(T t, E e,C c) {
		if(ObjectUtil.isNotNullEmpty(t)){
		    try {
		        //RepairEQPTSuggestExcelVO repairEQPTExcel = (RepairEQPTSuggestExcelVO)c;
	            String type = (String)PropertyUtils.getProperty(c, "eqptType_");
	            EQPTBaseService eQPTBaseService = (EQPTBaseService)SpringAppliction.getBean(EQPTBaseService.class);
	            
	            @SuppressWarnings("unchecked")
	            Object obj = eQPTBaseService.queryObjectByCustomNumber(EqptTypeEnum.getValueByLabel(type).getClazz(), (String)t);
	            
	            String pipeLineId = (String)DomainFieldReflectUtil.getFieldValue(obj, "pipeLineId");
	            HttpServletRequest request = RequestResponseContext.getRequest();
                String lineId_request = request.getParameter("pipeLineId");
                if(pipeLineId != null && lineId_request != null){//没有管道就不判断管道
                    if(!lineId_request.equals(pipeLineId)){//管道不同
                        return false;
                    }   
                }
                
                //设备的绝对距离
	            Long mileage = (Long)DomainFieldReflectUtil.getFieldValue(obj, "mileage");
	            if(mileage != null){

	                String fromPointId = request.getParameter("fromPointId");
	                String endPointId = request.getParameter("endPointId");
	                if(StringUtils.isNotEmpty(fromPointId) && StringUtils.isNotEmpty(endPointId)){
	                    PipeLocationService pipeLocationService = (PipeLocationService)SpringAppliction.getBean(PipeLocationService.class);
	                    PipeLocationDTO pipeLocation1 = pipeLocationService.queryPipeLocationById(pipeLineId,fromPointId);
	                    PipeLocationDTO pipeLocation2 = pipeLocationService.queryPipeLocationById(pipeLineId,endPointId);
	                    if(mileage.longValue() < pipeLocation1.getMileage2() || mileage.longValue() > pipeLocation2.getMileage2()){
	                        //在这个绝对距离范围内，没找到设备。
	                        return false;
	                    }
	                }
	                    
	            }
	            
	            
	            return obj != null;
            } catch (Exception e2) {
                return false;
            }
		    
		}
		return true;
	}

	@Override
	public String getName() {
		return null;
	}

}
